// Copyright (c) 2023 坚果派
// 公众号：nutpi
// 电话：17752170152
// 官网：https://www.nutpi.net/
// All rights reserved.

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:shared_preferences/shared_preferences.dart';
import '../services/log_service.dart';

// 简单的主题类型枚举
enum ThemeType { light, dark, green, blue, purple, orange }

class ThemeProvider extends ChangeNotifier {
  // 保存当前主题类型
  ThemeType _currentTheme = ThemeType.light;
  // 保留深色模式状态
  bool _isDarkMode = false;

  // 获取器方法
  ThemeType get currentTheme => _currentTheme;
  bool get isDarkMode => _isDarkMode;

  // 初始化加载保存的主题
  ThemeProvider() {
    _loadThemePreference();
  }

  // 加载主题首选项
  Future<void> _loadThemePreference() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      _isDarkMode = prefs.getBool('isDarkMode') ?? false;

      // 加载保存的主题类型
      final themeIndex = prefs.getInt('themeType') ?? 0;
      if (themeIndex < ThemeType.values.length) {
        _currentTheme = ThemeType.values[themeIndex];
      }

      notifyListeners();
    } catch (e) {
      LogService().error('加载主题设置时出错', e);
    }
  }

  // 切换深色模式
  Future<void> toggleTheme() async {
    _isDarkMode = !_isDarkMode;
    notifyListeners();

    try {
      final prefs = await SharedPreferences.getInstance();
      await prefs.setBool('isDarkMode', _isDarkMode);
    } catch (e) {
      LogService().error('保存主题设置时出错', e);
    }
  }

  // 设置新的主题类型
  Future<void> setTheme(ThemeType themeType) async {
    _currentTheme = themeType;
    notifyListeners();

    try {
      final prefs = await SharedPreferences.getInstance();
      await prefs.setInt('themeType', themeType.index);
    } catch (e) {
      LogService().error('保存主题类型时出错', e);
    }
  }

  // 获取当前应该使用的主题
  ThemeData getTheme() {
    switch (_currentTheme) {
      case ThemeType.light:
        return _isDarkMode ? _getDarkTheme() : _getLightTheme();
      case ThemeType.dark:
        return _getDarkTheme();
      case ThemeType.green:
        return _isDarkMode ? _getDarkGreenTheme() : _getLightGreenTheme();
      case ThemeType.blue:
        return _isDarkMode ? _getDarkBlueTheme() : _getLightBlueTheme();
      case ThemeType.purple:
        return _isDarkMode ? _getDarkPurpleTheme() : _getLightPurpleTheme();
      case ThemeType.orange:
        return _isDarkMode ? _getDarkOrangeTheme() : _getLightOrangeTheme();
      default:
        return _getLightTheme();
    }
  }

  // 各种主题定义
  ThemeData _getLightTheme() {
    return ThemeData.light().copyWith(
      primaryColor: Colors.green,
      scaffoldBackgroundColor: Colors.white,
      colorScheme: const ColorScheme.light(
        primary: Colors.green,
        secondary: Colors.lightGreen,
        tertiary: Colors.lime,
      ),
      appBarTheme: const AppBarTheme(
        backgroundColor: Colors.green,
        foregroundColor: Colors.white,
      ),
    );
  }

  ThemeData _getDarkTheme() {
    return ThemeData.dark().copyWith(
      primaryColor: Colors.green[700],
      scaffoldBackgroundColor: const Color(0xFF121212),
      colorScheme: ColorScheme.dark(
        primary: Colors.green[700]!,
        secondary: Colors.lightGreen[700]!,
        tertiary: Colors.lime[700]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.green[900],
        foregroundColor: Colors.white,
      ),
    );
  }

  ThemeData _getLightGreenTheme() {
    return ThemeData.light().copyWith(
      primaryColor: Colors.green[700],
      scaffoldBackgroundColor: Colors.green[50],
      colorScheme: ColorScheme.light(
        primary: Colors.green[700]!,
        secondary: Colors.lightGreen[600]!,
        tertiary: Colors.teal[500]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.green[700],
        foregroundColor: Colors.white,
      ),
      cardTheme: CardTheme(
        color: Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
      ),
    );
  }

  ThemeData _getDarkGreenTheme() {
    return ThemeData.dark().copyWith(
      primaryColor: Colors.green[800],
      scaffoldBackgroundColor: const Color(0xFF0A1F0A),
      colorScheme: ColorScheme.dark(
        primary: Colors.green[700]!,
        secondary: Colors.lightGreen[700]!,
        tertiary: Colors.teal[700]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.green[900],
        foregroundColor: Colors.white,
      ),
    );
  }

  ThemeData _getLightBlueTheme() {
    return ThemeData.light().copyWith(
      primaryColor: Colors.blue[700],
      scaffoldBackgroundColor: Colors.blue[50],
      colorScheme: ColorScheme.light(
        primary: Colors.blue[700]!,
        secondary: Colors.lightBlue[600]!,
        tertiary: Colors.cyan[500]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.blue[700],
        foregroundColor: Colors.white,
      ),
    );
  }

  ThemeData _getDarkBlueTheme() {
    return ThemeData.dark().copyWith(
      primaryColor: Colors.blue[800],
      scaffoldBackgroundColor: const Color(0xFF051530),
      colorScheme: ColorScheme.dark(
        primary: Colors.blue[700]!,
        secondary: Colors.lightBlue[700]!,
        tertiary: Colors.cyan[700]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.blue[900],
        foregroundColor: Colors.white,
      ),
    );
  }

  ThemeData _getLightPurpleTheme() {
    return ThemeData.light().copyWith(
      primaryColor: Colors.purple[700],
      scaffoldBackgroundColor: Colors.purple[50],
      colorScheme: ColorScheme.light(
        primary: Colors.purple[700]!,
        secondary: Colors.deepPurple[600]!,
        tertiary: Colors.indigo[500]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.purple[700],
        foregroundColor: Colors.white,
      ),
    );
  }

  ThemeData _getDarkPurpleTheme() {
    return ThemeData.dark().copyWith(
      primaryColor: Colors.purple[800],
      scaffoldBackgroundColor: const Color(0xFF190A30),
      colorScheme: ColorScheme.dark(
        primary: Colors.purple[700]!,
        secondary: Colors.deepPurple[700]!,
        tertiary: Colors.indigo[700]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.purple[900],
        foregroundColor: Colors.white,
      ),
    );
  }

  ThemeData _getLightOrangeTheme() {
    return ThemeData.light().copyWith(
      primaryColor: Colors.orange[700],
      scaffoldBackgroundColor: Colors.orange[50],
      colorScheme: ColorScheme.light(
        primary: Colors.orange[700]!,
        secondary: Colors.deepOrange[600]!,
        tertiary: Colors.amber[500]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.orange[700],
        foregroundColor: Colors.white,
      ),
    );
  }

  ThemeData _getDarkOrangeTheme() {
    return ThemeData.dark().copyWith(
      primaryColor: Colors.orange[800],
      scaffoldBackgroundColor: const Color(0xFF301A05),
      colorScheme: ColorScheme.dark(
        primary: Colors.orange[700]!,
        secondary: Colors.deepOrange[700]!,
        tertiary: Colors.amber[700]!,
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: Colors.orange[900],
        foregroundColor: Colors.white,
      ),
    );
  }
}
